package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;


import com.ldj.hibernate.NoticeInfo;
import com.ldj.hibernate.NoticeInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.NoticeInfoForm;

public class NoticeInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			NoticeInfoForm nForm = (NoticeInfoForm)form;
			NoticeInfoDAO dao = new NoticeInfoDAO(request, response);
			String startDate = request.getParameter("noticeInfoStartDate");
			String endDate = request.getParameter("noticeInfoEndDate");
			List list = dao.findAllPage(startDate,endDate,nForm);
			request.setAttribute("list", list);
			return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoticeInfoForm nForm = (NoticeInfoForm)form;
		NoticeInfo info = new NoticeInfo();
		info.setNoticeInfoTitle(nForm.getNoticeInfoTitle());
		info.setNoticeInfoType(nForm.getNoticeInfoType());
		info.setNoticeInfoSource((nForm.getNoticeInfoSource()));
		info.setNoticeInfoContent(nForm.getNoticeInfoContent());
		info.setNoticeInfoDelOper("0");	
		info.setNoticeInfoDate(new Date());	
//		info.setNoticeInfoName(SystemSession.getSupUserId(request));
//		PersionFileDAO persionFiledao = new PersionFileDAO(request, response);
//		PersionFile persionInfo = persionFiledao.findById(SystemSession.getSupUserId(request));
		info.setPersionFile(SystemSession.getSupUserInfo(request));
		NoticeInfoDAO noticeInfoDao = new NoticeInfoDAO(request, response);
		noticeInfoDao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NoticeInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getNoticeInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NoticeInfo.do?method=list", true);
		//return new ActionForward("/NoticeInfo.do?method=add");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoticeInfoForm nForm = (NoticeInfoForm)form;
		NoticeInfoDAO noticeInfoDao = new NoticeInfoDAO(request, response);
		NoticeInfo ninfo = noticeInfoDao.findById(nForm.getNoticeInfoId());
		request.setAttribute("ninfo", ninfo);
		return mapping.findForward("detail");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		NoticeInfoForm nForm = (NoticeInfoForm)form;
		NoticeInfoDAO dao = new NoticeInfoDAO(request, response);
		NoticeInfo info = dao.findById(nForm.getNoticeInfoId());
		request.setAttribute("nInfo", info);		
		return mapping.findForward("toUpdate");
	
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoticeInfoForm nForm = (NoticeInfoForm)form;
		NoticeInfoDAO dao = new NoticeInfoDAO(request, response);
		NoticeInfo info = dao.findById(nForm.getNoticeInfoId());
		info.setNoticeInfoContent(nForm.getNoticeInfoContent());
		info.setNoticeInfoType(nForm.getNoticeInfoType());
		info.setNoticeInfoSource((nForm.getNoticeInfoSource()));
		info.setNoticeInfoTitle(nForm.getNoticeInfoTitle());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NoticeInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getNoticeInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NoticeInfo.do?method=list", true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoticeInfoForm nForm = (NoticeInfoForm)form;
		NoticeInfoDAO NoticeInfoDao = new NoticeInfoDAO(request, response);
		NoticeInfo ninfo = NoticeInfoDao.findById(nForm.getNoticeInfoId());
		ninfo.setNoticeInfoDelOper("1");
		NoticeInfoDao.merge(ninfo);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "NoticeInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", ninfo.getNoticeInfoTitle()));
		logDao.save(s);
		return new ActionForward("/NoticeInfo.do?method=list", true);
	}

}